﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms;

namespace CommonLibrary.CustControls
{
    class CusEdits : UITypeEditor
    {
        // 重写获得样式
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            // 如果上下文不为空且上下文实例不为空 则返回省略号样式
            if (context != null && context.Instance != null)
            {
                return UITypeEditorEditStyle.Modal;
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            //// 此法由于要引入程序集 WfDesignBD 所以有待考证
            BD_DataGridView grdView = new BD_DataGridView();
            WfDesignBD.CustomProperty cp = context.Instance as WfDesignBD.CustomProperty;
            if (cp != null)
            {
                grdView = cp.GetCurrentSelectObject() as BD_DataGridView;
            }

            if (context != null && context.Instance != null)
            {
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    FrmEditCols frmEditCols = new FrmEditCols(grdView);
                    if (frmEditCols.ShowDialog() == DialogResult.OK)
                    {
                        value = frmEditCols.GetColumns();
                        //var cols = value as DataGridViewColumnCollection;
                        //foreach (DataGridViewTextBoxColumn col in cols)
                        //{
                        //    var co = col as ExternalPropTextBoxColumn;
                        //}
                    }

                    return value;
                }
            }
            return base.EditValue(context, provider, value);
        }
    }

    class DataSourceEdits : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (context != null && context.Instance != null)
            {
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    String sql = value == null ? null : value.ToString();
                    FrmSQL frmSQL = FrmSQL.CreateFrmInstace(sql);
                    if (frmSQL.ShowDialog() == DialogResult.OK)
                    {
                        value = frmSQL.GetSQL();
                    }
                    return value;
                }
            }
            return base.EditValue(context, provider, value);
        }

    }

    class ImageEdits : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null && context.Instance != null)
            {
                return UITypeEditorEditStyle.Modal;
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            if (context != null && context.Instance != null)
            {
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    OpenFileDialog openFile = new OpenFileDialog();
                    openFile.RestoreDirectory = false;
                    openFile.Filter = "JPGE|*.jpg|BMP|*.bmp|所有文件|*.*";
                    if (openFile.ShowDialog() == DialogResult.OK)
                    {
                        value = openFile.FileName;
                    }
                    return value;
                }
            }
            return base.EditValue(context, provider, value);
        }
    }

    class DataBindingEdits : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null && context.Instance != null)
            {
                return UITypeEditorEditStyle.Modal;
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            BD_DataGridView grdView = new BD_DataGridView();
            WfDesignBD.CustomProperty cp = context.Instance as WfDesignBD.CustomProperty;
            if (cp != null)
                grdView = cp.GetCurrentSelectObject() as BD_DataGridView;

            if (context != null && context.Instance != null)
            {
                editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (editorService != null)
                {
                    FrmAddData frmAddData = new FrmAddData(grdView);
                    if (frmAddData.ShowDialog() == DialogResult.OK)
                    {
                        value = frmAddData.GetDataGridView();
                    }
                    return value;
                }
            }

            return base.EditValue(context, provider, value);
        }
    }

    class FormularEdits : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null && context.Instance != null)
            {
                return UITypeEditorEditStyle.Modal;
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            BD_TextBox txtbox = new BD_TextBox();
            WfDesignBD.CustomProperty cp = context.Instance as WfDesignBD.CustomProperty;
            if (cp != null)
                txtbox = cp.GetCurrentSelectObject() as BD_TextBox;

            Control form = txtbox.Parent;
            var ctrls = form.Controls;
            foreach (var ctrl in ctrls)
            {
                if (ctrl.GetType() == typeof(BD_TextBox))
                {
                    var textbox = (BD_TextBox)ctrl;
                    if (textbox.Name == String.Empty)
                    {
                        MessageBox.Show("存在为命名文本框,请确认");
                        return base.EditValue(context, provider, value);
                    }
                }
            }

            if (context != null && context.Instance != null)
            {
                String formular = value == null ? null : value.ToString();
                FrmFormular frmFormular = new FrmFormular(form, txtbox, formular);
                if (frmFormular.ShowDialog() == DialogResult.OK)
                {
                    value = frmFormular.getFormular();
                    return value;
                }
            }

            return base.EditValue(context, provider, value);
        }
    }
}